<?php
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cli\IniData\GoodsActionCounterFun;
use Biz\model\GoodsActionCount;
use Biz\model\GoodsHeatDegree;
use Biz\service\esSearch\Goods;

//取前一天商品的操作数
$currentDate = date("Y-m-d");

$dateRange = [
	date("Y-m-d", strtotime($currentDate) - 86400),
	$currentDate
];
//GoodsActionCounterFun::InitData($dateRange);
//
//
//$goodsActionInfos = GoodsActionCount::select('*', ['add_date' => $dateRange[0]]);
//查询统计数据
$goodsActionInfos = \Biz\model\DbStatJson::getInfos();


$fieldsTypeMap = [
	'pv' => GoodsActionCount::DEGREE_TYPE_BROWSE,
	'fav_count' => GoodsActionCount::DEGREE_TYPE_FAV,
	'add_cart_count' => GoodsActionCount::DEGREE_TYPE_ADD_CART,
	'pay_order_count' => GoodsActionCount::DEGREE_TYPE_ORDER
];

$heatDegree = [];

// 根据操作数计算热度值
foreach ($goodsActionInfos as $goodsActionInfo) {
	foreach ($goodsActionInfo as $field => $count) {
		if (isset($fieldsTypeMap[$field]) && $count) {
			$heatDegreeOnType = $count * 31 * GoodsHeatDegree::ACTION_COUNT_HEAT_MAP[$fieldsTypeMap[$field]]; //计算一天前的 各维度 热度值
			$heatDegree[$goodsActionInfo['spu_id']][$fieldsTypeMap[$field]] =  $heatDegreeOnType;
		}
	}
}
echo "更新的数据:" . json_encode($heatDegree, JSON_UNESCAPED_UNICODE);
$upGoodsIds = [];
//更新昨天有变化的 商品 热度值
foreach ($heatDegree as $goodsId  => $degrees) {
    try {
        $allHeatDegree = GoodsHeatDegree::getCurrentDegree($goodsId);

        $realDegree = array_sum($degrees);
        if ($allHeatDegree) {
            $realDegree = $realDegree + ($allHeatDegree * 30 / 29);
        }

        // 防多次执行
        $exists = GoodsHeatDegree::get('*', ['goods_id' => $goodsId, 'add_date' => $dateRange[0] , 'status' => GoodsHeatDegree::STAUTS_NORMAL]);

        if (! $exists)
        {
            GoodsHeatDegree::action(function () use ($goodsId, $realDegree, $dateRange) {
                GoodsHeatDegree::update(['status' => GoodsHeatDegree::STAUTS_DEL],[
                    'goods_id' => $goodsId,
                    'status' => GoodsHeatDegree::STAUTS_NORMAL
                ]);

                GoodsHeatDegree::insert([
                    'goods_id' => $goodsId,
                    'heat_degree' => $realDegree,
                    'add_date' => $dateRange[0],
                    'status' => GoodsHeatDegree::STAUTS_NORMAL
                ]);

            });
            $upGoodsIds[] = $goodsId;
        }
    } catch (Exception $e) {
        echo '热度值变化::错误,goodsId:'.$goodsId . '信息:'.$e->getMessage() . PHP_EOL;
    }
}

//获取超过 30 以上 没有热度值没有变化的商品 状态删除 更新 solr
$thirtyDateAgo = date('Y-m-d', time() - 30 * 86400);
$goodsDegreeInfos = GoodsHeatDegree::select('*', [
	'add_date[<]' => $thirtyDateAgo,
	'status' => GoodsHeatDegree::STAUTS_NORMAL
]);

foreach ($goodsDegreeInfos as $goodsDegreeInfo) {
    try {
        GoodsHeatDegree::update(['status' => GoodsHeatDegree::STAUTS_DEL] , [
            'id' => $goodsDegreeInfo['id'],
        ]);
        $upGoodsIds[] = $goodsDegreeInfo['goods_id'];
    } catch (Exception $e) {
        echo '30天热度值无变化::错误,goodsId:'.$goodsId . '信息:'.$e->getMessage() . PHP_EOL;
    }
}

//更新solr es
$upGoodsIds = array_unique($upGoodsIds);
foreach ($upGoodsIds as $goodsId) {
    try {
        \Biz\service\search\Goods::updateByGoodsId($goodsId);

        //同步数据到es
        $goodsInfo = [
            'id' => $goodsId,
            'heat_degree' => GoodsHeatDegree::getCurrentDegree($goodsId),
        ];
        saveByGoodsId($goodsInfo);
    } catch (\Exception $e) {
        echo '更新solr es::错误,goodsId:'.$goodsId . '信息:'.$e->getMessage() . PHP_EOL;
    }
}

function saveByGoodsId($goodsInfo)
{
    Goods::saveByGoodsId($goodsInfo);
}
